Title Banner

Previous Book Contents Book Index Next

Inside Macintosh: QuickDraw GX Graphics /
Chapter 4 - Geometric Operations / Using Geometric Operations


Performing Geometric Arithmetic With Shapes

QuickDraw GX provides six arithmetic operations you can apply to geometric shapes: union, intersection, difference, reverse difference, exclusion, and inversion.

To illustrate these operations, the sample function in Listing 4-12 creates two shapes: a diamond-shaped polygon and a circular path.

Listing 4-12 Creating a diamond-shaped polygon and a circular path that intersect

void CreateDiamondAndCircle(void)
{
   gxShape  diamondShape, circleShape;

   static long circleGeometry[] = {1, /* number of contours */
                                   4, /* number of points */
                                   0xF0000000, /* 1111 ... */
                                   ff(100), ff(100),  /* off */
                                   ff(200), ff(100),  /* off */
                                   ff(200), ff(200),  /* off */ 
                                   ff(100), ff(200)}; /* off */

   static long diamondGeometry[] = {1, /* number of contours */
                                    4, /* number of points */
                                    ff(50), ff(150),
                                    ff(100), ff(100),
                                    ff(150), ff(150),
                                    ff(100), ff(200)};

   diamondShape = GXNewPolygons((gxPolygons *) diamondGeometry);
   
   circleShape = GXNewPaths((gxPaths *) circleGeometry);

   GXDrawShape(diamondShape);
   GXDisposeShape(diamondShape);
   GXDrawShape(circleShape);
   GXDisposeShape(circleShape);
}
The resulting shapes are shown in Figure 4-51.

Figure 4-51 A diamond-shaped polygon geometry and a circular path geometry

The GXIntersectShape function finds the area common to two shapes. This function takes two parameters--the shapes to intersect--and stores the result in the first parameter.

If you apply the GXIntersectShape function to the diamond-shaped polygon and the circular path from Listing 4-12 by calling

GXIntersectShape(diamondShape, circleShape);
GXDrawShape(diamondShape);
you get the resulting shape shown in Figure 4-52.

Figure 4-52 The intersection of a diamond-shaped polygon and a circular path

Implementation Note
Due to a implementation limit with QuickDraw GX version 1.0, you can find the intersection of two framed shapes only if the shapes are points, lines, or curves. You can, however, find the intersection of a framed shape and a filled shape; the intersection is the part of the framed shape contained in the filled shape. In this case, the target shape must be the framed shape and the operand shape must be the filled shape.
You can find the intersection of two rectangle geometries--without having to encapsulate those geometries into shapes--using the GXIntersectRectangle function, which is described on page 4-105. Similarly, you can find the union of two rectangle geometries (which is the considered to be the smallest rectangle that contains them both) using the GXUnionRectangle function, which is described on page 4-106.

The GXUnionShape function combines the areas covered by two shapes. This function also takes two parameters--the shapes to combine--and stores the result in the first parameter.

If you apply the GXUnionShape function to the diamond-shaped polygon and the circular path from Listing 4-12 by calling

GXUnionShape(diamondShape, circleShape);
GXDrawShape(diamondShape);
you get the resulting shape shown in Figure 4-53.

Figure 4-53 The union of a diamond-shaped polygon and a circular path

Although you cannot find the union of a framed shape and a solid shape, you can find the union of two framed shapes. If the diamond-shaped polygon and the circular path from Listing 4-12 had closed-frame fills, the resulting union would appear as shown in Figure 4-54.

Figure 4-54 The union of a framed diamond-shaped polygon and a circular path

The GXDifferenceShape function subtracts the area of one shape from the area of another. This function takes two parameters--the shape to subtract from and the shape to subtract--and stores the result in the first parameter.

If you apply the GXDifferenceShape function to the diamond-shaped polygon and the circular path from Listing 4-12 by calling

GXDifferenceShape(diamondShape, circleShape);
GXDrawShape(diamondShape);
you get the resulting shape shown in Figure 4-55.

Figure 4-55 The result of subtracting a circular path from a diamond-shaped polygon

The GXReverseDifferenceShape function is similar to the GXDifferenceShape function, except the GXReverseDifferenceShape function subtracts the first parameter from the second parameter. Like GXDifferenceShape, it also stores the result in the first parameter.

If you apply the GXReverseDifferenceShape function to the diamond-shaped polygon and the circular path from Listing 4-12 by calling

GXReverseDifferenceShape(diamondShape, circleShape);
GXDrawShape(diamondShape);
you get the resulting shape shown in Figure 4-56.

Figure 4-56 The result of subtracting a diamond-shaped polygon from a circular path

The GXExcludeShape function performs the exclusive-OR operation on the areas of two shapes--that is, it finds the area that is covered by one shape or the other, but not by both shapes.

If you apply the GXExcludeShape function to the diamond-shaped polygon and the circular path from Listing 4-12 by calling

GXExcludeShape(diamondShape, circleShape);
GXDrawShape(diamondShape);
you get the resulting shape shown in Figure 4-57.

Figure 4-57 The result of the exclusive-OR operation on a polygon and a path

Finally, QuickDraw GX provides the GXInvertShape function which inverts the area covered by a shape--that is, the resulting shape covers all of the area not covered by the original shape. This function takes one parameter--the shape to invert--and stores the result in this parameter.

If you apply the GXInvertShape function to the diamond-shaped polygon from
Listing 4-12 by calling

GXInvertShape(diamondShape);
you get the resulting shape shown in Figure 4-58. Notice that this shape extends to the full extent of its view port.

Figure 4-58 An inverted diamond

For more information about these arithmetic operations, see the function descriptions in "Performing Geometric Arithmetic With Shapes" beginning on page 4-104.


Previous Book Contents Book Index Next

© Apple Computer, Inc.
7 JUL 1996




Navigation graphic, see text links

Main | Page One | What's New | Apple Computer, Inc. | Find It | Contact Us | Help